/*
 * Created on 2006-5-15
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package kerjodando.vincent;

import java.util.List;

import org.apache.log4j.Logger;

import ants.p2p.NeighbourAnt;
import ants.p2p.filesharing.WarriorAnt;
import ants.p2p.gui.FrameAnt;
import ants.p2p.gui.SettingsAntPanel;
import ants.p2p.security.EndpointSecurityManager;
import ants.p2p.utils.addresses.InetAddressEngine;
import ants.p2p.utils.addresses.InetAddressWatchdog;

/**
 * @author vicent(zhu1230@gmail.com)
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class Antsp2pManager {
	private static final Logger logger = Logger.getLogger(Antsp2pManager.class);

	/*
	 * public void updateIpfilter() {
	 * 
	 * InetAddressWatchdog.getInstance().loadFilterFile( WarriorAnt.workingPath +
	 * "ipfilter.dat");
	 *  }
	 */

	public void updateSettings() {
		SettingsAntPanel.loadStaticSettings();
		FrameAnt.getInstance("").getGuiAnt().getSettingsAntPanel()
				.updatesettingsUI();
	}

	public void updateTrustip() {
		InetAddressEngine.loadTrustedPeers();
		FrameAnt.getInstance("").getGuiAnt().getConnectionAntPanel()
				.updatetrustedpeersUI();
	}

	public void checkpeers() throws Exception {
		logger.info("checkpeers()");
		if (FrameAnt.getInstance("").getGuiAnt().getConnectionAntPanel()
				.getWarriorAnt() == null) {
			return;
		}
		List neighbour = FrameAnt.getInstance("").getGuiAnt()
				.getConnectionAntPanel().getWarriorAnt().getNeighbours();
		// logger.info("start neighbour��its number is "+neighbour.size()+",and
		// start checking PEERS");
		for (int i = 0; i < neighbour.size(); i++) {
			logger.info("peers, No.=>" + i);
			NeighbourAnt na = (NeighbourAnt) neighbour.get(i);
			String str[] = na.getRemoteId().split("  ");
			logger.info("RemoteId is" + na.getRemoteId() + "|first ip=>"
					+ str[0] + "|second ip=>" + str[1]);
			String ip = str[0];

			if (!InetAddressWatchdog.getInstance().allowedAddress(ip)) {
				logger.info("ip " + ip + "|is forbiden");
				na.terminate();
				neighbour.remove(i);
				logger.info("forbiden finish");
			}

		}

		/* removed by casper */
		/*
		 * //logger.info("start outputSecure"); java.util.List outputSecure =
		 * FrameAnt.getInstance("").getGuiAnt()
		 * .getConnectionAntPanel().getWarriorAnt().outputSecureConnections;
		 * //logger.info("outputSecure's number is "+outputSecure.size()); for
		 * (int i = 0; i < outputSecure.size(); i++) { logger.info("start
		 * outputSecure i=>" + i); EndpointSecurityManager esm =
		 * (EndpointSecurityManager) outputSecure .get(i);
		 * logger.info("peerid=>" + esm.getPeerId()); String[] ip =
		 * esm.getPeerId().split(" "); logger.info("str1=>" + ip[0] + "|str2=>" +
		 * ip[1]); if (!InetAddressWatchdog.getInstance().allowedAddress(ip[0])) {
		 * logger.info("ip=>" + ip[0] + "is forbiden"); esm.interrupt();
		 * outputSecure.remove(esm.getPeerId()); } } //logger.info("end
		 * outputSecre"); //logger.info("start inputSecure"); List inputSecure =
		 * FrameAnt.getInstance("").getGuiAnt()
		 * .getConnectionAntPanel().getWarriorAnt().inputSecureConnections;
		 * //logger.info("inputSecure's number is "+inputSecure.size()); for
		 * (int i = 0; i < inputSecure.size(); i++) { logger.info("start
		 * inputSecure i=>" + i); EndpointSecurityManager esm =
		 * (EndpointSecurityManager) inputSecure .get(i); logger.info("peerid=>" +
		 * esm.getPeerId()); String[] ip = esm.getPeerId().split(" ");
		 * logger.info("str1=>" + ip[0] + "|str2=>" + ip[1]); if
		 * (!InetAddressWatchdog.getInstance().allowedAddress(ip[0])) {
		 * logger.info("ip=>" + ip[0] + "is forbiden"); esm.interrupt();
		 * inputSecure.remove(esm.getPeerId()); } } //logger.info("end
		 * inputSecure");
		 */
	}
}
